#include"adddate.h"

addDate::addDate(){
    importDate = "";
}
addDate::~addDate(){

}







//连接数据库
void addDate::connect_db(QString furnWin ,QString furnNum ,  QString tubeNum , QString temp , QString time)
{
    QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("schema1");
    db.setUserName("root");
    db.setPassword("");
    //检测连接状态
    if(db.open()){

        qDebug()<<"database is established!";
    }
    else{
        qDebug()<<"faled to connect to database";
    }



    if(furnWin=="1"||furnWin=="4"||furnWin=="5"||furnWin=="8"){
        this->addInTempData(furnNum,tubeNum,temp,time);
    }
    if(furnWin=="2"||furnWin=="3"||furnWin=="6"||furnWin=="7"){
        this->addOutTempData(furnNum,tubeNum,temp,time);
    }



    //断开连接
    db.close();
}

//添加数据到数据库
void addDate::addInTempData(QString furnNum ,  QString tubeNum , QString temp , QString time)
{
    qDebug() << "sql for InData" << furnNum << tubeNum << temp << time ;
    //    QDate d;
    //    d = QDateTime::fromString(time, "yyyy-MM-dd hh:mm:ss");
    QSqlRecord rec;
    rec.clear();
    QSqlQuery query;
    QString str ="INSERT INTO `schema1`.`table_in` (`FN`, `TN`,`Temp`,`Time`) VALUES ('"+furnNum +"','"+tubeNum+"','"+temp+"','"+ time +"')";
    query.exec(str);

}
void addDate::addOutTempData(QString furnNum ,  QString tubeNum , QString temp , QString time)
{
    qDebug() << "sql for OutData" << furnNum << tubeNum << temp << time ;
    //    QDate d;
    //    d = QDateTime::fromString(time, "yyyy-MM-dd hh:mm:ss");
    QSqlRecord rec;
    rec.clear();
    QSqlQuery query;
    QString str ="INSERT INTO `schema1`.`table_out` (`FN`, `TN`,`Temp`,`Time`) "
                 "VALUES ('"+furnNum +"','"+tubeNum+"','"+temp+"','"+ time +"')";
    query.exec(str);
}

//url接口
QString addDate::forurl(QString file_1)
{
    QString importTime;

    QDir dir(file_1);
    QString time1  = file_1.mid(file_1.count()-8,file_1.count());
    //    qDebug()<<time1;
    qDebug()<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
    qDebug()<< file_1;
    qDebug()<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
    //    遍历所有文件夹内所有路径

    for(int a=0;a<dir.entryInfoList().count();a++)
    {

        //        QFileInfo::absoluteFilePath为返回绝对路径，包括名字

        QFileInfo my_info;
        my_info = dir.entryInfoList().at(a);
        qDebug() << my_info.absoluteFilePath();
        QString time2 = my_info.absoluteFilePath();
        qDebug()<<"@@@@@@@@@@@@@@@@@@@@@@@@@@";
        qDebug()<<time2;
        qDebug()<<"@@@@@@@@@@@@@@@@@@@@@@@@@@";
        if(time2.right(4)!=".txt")
            continue;
        //        time2 = time2.mid(time2.count()-21,time2.count());
        time2 = time2.right(30);
        qDebug()<<"````````````````````````````````````````````````````````````````````````````````````";
        qDebug()<<time2;
        qDebug()<<"`````````````````````````````````````````````````````````````````````````````````````";

        QStringList time22 = time2.split(QRegExp("[/``-]"));
        QString check1 = time22.at(4).left(1);
        QString check2 = time22.at(4).mid(4,1);
        if(check1!="S"&&check2!="W")
            return "";
        QString time222=time22.at(1)+":"+time22.at(2)+":"+time22.at(3);
        QString time101 = time1;
        QString time102 = time1;
        QString time103 = time1;
        QString time11=time101.left(4);
        QString time12=time102.remove(0,4).remove(2,2);
        QString time13=time103.right(2);
        QString time111 = time11+"-"+time12+"-"+time13;
        QString time = time111+" "+time222;
        importTime = time111;
        qDebug()<<"######################";
        qDebug()<<time;
        qDebug()<<"#######################";

        QFile *file = new QFile();
        file->setFileName(my_info.absoluteFilePath());

        file->open(QIODevice::ReadWrite | QIODevice::Text/*|QIODevice::Append*/);
        QByteArray line01 = file->readLine();
        QString tempDate(line01);



        //        qDebug()<<"~~~~~~~~~~~~~~~~~~";
        //        qDebug()<<tempDate;
        //        qDebug()<<"~~~~~~~~~~~~~~~~~~";



        QStringList aa1 = tempDate.split(QRegExp("[??#,,,,,,,,,,,,,??]"));
        if(tempDate.right(3)!="@@@")
        {
            QString furnNum = aa1.at(2);
            QString furnWin = aa1.at(3);
            QString temp1,temp2,temp3,temp4,temp5,temp6,temp7,temp8,temp9,temp10,temp11,temp12;
            temp1 = aa1.at(4);
            temp2 = aa1.at(5);
            temp3 = aa1.at(6);
            temp4 = aa1.at(7);
            temp5 = aa1.at(8);
            temp6 = aa1.at(9);
            temp7 = aa1.at(10);
            temp8 = aa1.at(11);
            temp9 = aa1.at(12);
            temp10 = aa1.at(13);
            temp11 = aa1.at(14);
            temp12 = aa1.at(15);
            qDebug()<<furnNum;
            qDebug()<<furnWin;
            qDebug()<<temp1<<temp2<<temp3<<temp4<<temp5<<temp6<<temp7<<temp8<<temp9<<temp10<<temp11<<temp12;
            //顺
            if(furnWin=="1")
            {
                qDebug()<<"&&&&&&&&&&&&&&&&&&&&&&1111111111111111111&&&&&&&&&&&&&&&&&&&&&&&&&";
                this->connect_db(furnWin,furnNum,"37",temp1,time);
                this->connect_db(furnWin,furnNum,"38",temp2,time);
                this->connect_db(furnWin,furnNum,"39",temp3,time);
                this->connect_db(furnWin,furnNum,"40",temp4,time);
                this->connect_db(furnWin,furnNum,"41",temp5,time);
                this->connect_db(furnWin,furnNum,"42",temp6,time);
                this->connect_db(furnWin,furnNum,"43",temp7,time);
                this->connect_db(furnWin,furnNum,"44",temp8,time);
                this->connect_db(furnWin,furnNum,"45",temp9,time);
                this->connect_db(furnWin,furnNum,"46",temp10,time);
                this->connect_db(furnWin,furnNum,"47",temp11,time);
                this->connect_db(furnWin,furnNum,"48",temp12,time);
            }
            //逆
            if(furnWin=="2")
            {
                this->connect_db(furnWin,furnNum,"37",temp12,time);
                this->connect_db(furnWin,furnNum,"38",temp11,time);
                this->connect_db(furnWin,furnNum,"39",temp10,time);
                this->connect_db(furnWin,furnNum,"40",temp9,time);
                this->connect_db(furnWin,furnNum,"41",temp8,time);
                this->connect_db(furnWin,furnNum,"42",temp7,time);
                this->connect_db(furnWin,furnNum,"43",temp6,time);
                this->connect_db(furnWin,furnNum,"44",temp5,time);
                this->connect_db(furnWin,furnNum,"45",temp4,time);
                this->connect_db(furnWin,furnNum,"46",temp3,time);
                this->connect_db(furnWin,furnNum,"47",temp2,time);
                this->connect_db(furnWin,furnNum,"48",temp1,time);
                qDebug()<<"&&&&&&&&&&&&&&&&&&&&&&2222222222222222222&&&&&&&&&&&&&&&&&&&&&&&&&";
            }
            //顺
            if(furnWin=="3")
            {
                this->connect_db(furnWin,furnNum,"25",temp1,time);
                this->connect_db(furnWin,furnNum,"26",temp2,time);
                this->connect_db(furnWin,furnNum,"27",temp3,time);
                this->connect_db(furnWin,furnNum,"28",temp4,time);
                this->connect_db(furnWin,furnNum,"29",temp5,time);
                this->connect_db(furnWin,furnNum,"30",temp6,time);
                this->connect_db(furnWin,furnNum,"31",temp7,time);
                this->connect_db(furnWin,furnNum,"32",temp8,time);
                this->connect_db(furnWin,furnNum,"33",temp9,time);
                this->connect_db(furnWin,furnNum,"34",temp10,time);
                this->connect_db(furnWin,furnNum,"35",temp11,time);
                this->connect_db(furnWin,furnNum,"36",temp12,time);
                qDebug()<<"&&&&&&&&&&&&&&&&&&&&&&33333333333333333333&&&&&&&&&&&&&&&&&&&&&&&&&";
            }
            //逆
            if(furnWin=="4")
            {
                this->connect_db(furnWin,furnNum,"25",temp12,time);
                this->connect_db(furnWin,furnNum,"26",temp11,time);
                this->connect_db(furnWin,furnNum,"27",temp10,time);
                this->connect_db(furnWin,furnNum,"28",temp9,time);
                this->connect_db(furnWin,furnNum,"29",temp8,time);
                this->connect_db(furnWin,furnNum,"30",temp7,time);
                this->connect_db(furnWin,furnNum,"31",temp6,time);
                this->connect_db(furnWin,furnNum,"32",temp5,time);
                this->connect_db(furnWin,furnNum,"33",temp4,time);
                this->connect_db(furnWin,furnNum,"34",temp3,time);
                this->connect_db(furnWin,furnNum,"35",temp2,time);
                this->connect_db(furnWin,furnNum,"36",temp1,time);
                qDebug()<<"&&&&&&&&&&&&&&&&&&&&&&444444444444444444444&&&&&&&&&&&&&&&&&&&&&&&&&";
            }
            //顺
            if(furnWin=="5")
            {
                this->connect_db(furnWin,furnNum,"13",temp1,time);
                this->connect_db(furnWin,furnNum,"14",temp2,time);
                this->connect_db(furnWin,furnNum,"15",temp3,time);
                this->connect_db(furnWin,furnNum,"16",temp4,time);
                this->connect_db(furnWin,furnNum,"17",temp5,time);
                this->connect_db(furnWin,furnNum,"18",temp6,time);
                this->connect_db(furnWin,furnNum,"19",temp7,time);
                this->connect_db(furnWin,furnNum,"20",temp8,time);
                this->connect_db(furnWin,furnNum,"21",temp9,time);
                this->connect_db(furnWin,furnNum,"22",temp10,time);
                this->connect_db(furnWin,furnNum,"23",temp11,time);
                this->connect_db(furnWin,furnNum,"24",temp12,time);
                qDebug()<<"&&&&&&&&&&&&&&&&&&&&&&555555555555555555551&&&&&&&&&&&&&&&&&&&&&&&&&";
            }
            //逆
            if(furnWin=="6")
            {
                this->connect_db(furnWin,furnNum,"13",temp12,time);
                this->connect_db(furnWin,furnNum,"14",temp11,time);
                this->connect_db(furnWin,furnNum,"15",temp10,time);
                this->connect_db(furnWin,furnNum,"16",temp9,time);
                this->connect_db(furnWin,furnNum,"17",temp8,time);
                this->connect_db(furnWin,furnNum,"18",temp7,time);
                this->connect_db(furnWin,furnNum,"19",temp6,time);
                this->connect_db(furnWin,furnNum,"20",temp5,time);
                this->connect_db(furnWin,furnNum,"21",temp4,time);
                this->connect_db(furnWin,furnNum,"22",temp3,time);
                this->connect_db(furnWin,furnNum,"23",temp2,time);
                this->connect_db(furnWin,furnNum,"24",temp1,time);
                qDebug()<<"&&&&&&&&&&&&&&&&&&&&&&666666666666666666666&&&&&&&&&&&&&&&&&&&&&&&&&";
            }
            //顺
            if(furnWin=="7")
            {
                this->connect_db(furnWin,furnNum,"1",temp1,time);
                this->connect_db(furnWin,furnNum,"2",temp2,time);
                this->connect_db(furnWin,furnNum,"3",temp3,time);
                this->connect_db(furnWin,furnNum,"4",temp4,time);
                this->connect_db(furnWin,furnNum,"5",temp5,time);
                this->connect_db(furnWin,furnNum,"6",temp6,time);
                this->connect_db(furnWin,furnNum,"7",temp7,time);
                this->connect_db(furnWin,furnNum,"8",temp8,time);
                this->connect_db(furnWin,furnNum,"9",temp9,time);
                this->connect_db(furnWin,furnNum,"10",temp10,time);
                this->connect_db(furnWin,furnNum,"11",temp11,time);
                this->connect_db(furnWin,furnNum,"12",temp12,time);
                qDebug()<<"&&&&&&&&&&&&&&&&&&&&&&777777777777777777777777&&&&&&&&&&&&&&&&&&&&&&&&&";
            }
            //逆
            if(furnWin=="8")
            {
                this->connect_db(furnWin,furnNum,"1",temp12,time);
                this->connect_db(furnWin,furnNum,"2",temp11,time);
                this->connect_db(furnWin,furnNum,"3",temp10,time);
                this->connect_db(furnWin,furnNum,"4",temp9,time);
                this->connect_db(furnWin,furnNum,"5",temp8,time);
                this->connect_db(furnWin,furnNum,"6",temp7,time);
                this->connect_db(furnWin,furnNum,"7",temp6,time);
                this->connect_db(furnWin,furnNum,"8",temp5,time);
                this->connect_db(furnWin,furnNum,"9",temp4,time);
                this->connect_db(furnWin,furnNum,"10",temp3,time);
                this->connect_db(furnWin,furnNum,"11",temp2,time);
                this->connect_db(furnWin,furnNum,"12",temp1,time);
                qDebug()<<"&&&&&&&&&&&&&&&&&&&&&&888888888888888888888&&&&&&&&&&&&&&&&&&&&&&&&&";
            }


            file->write("@@@");
        }
        file->close();


    }

return importTime;
}
